<?php
    //图片列表
    // $list=array(
    //     'uploads/1.jpg',
    //     'uploads/2.jpg',
    //     'uploads/3.jpg',
    //     'uploads/4.jpg',
    //     'uploads/5.png',
    //     'uploads/6.png'
    // );
    //遍历目录,获取图片列表
    $dir=scandir('uploads/');
    foreach($dir as $item){
        if(!in_array($item,array('.','..'))){
            $list[]='uploads/'.$item;
        }
    }

    if(isset($_GET['img'])){
        //获取用户要下载的文件
        $img=$_GET['img'];
        //获取图片后缀名
        $ext=strrchr($img,'.');
        //强制下载,无法指定下载文件名称(会以当前文件名作为默认名称)
        // header('Content-Type:application/octet-stream;');
        //1-强制下载;同时设置下载文件名称
        header('Content-Disposition: attachment; filename="'.time().$ext.'"');
        // header('Content-Type:image/'.substr($ext,1));
        //2-读取用户要下载的图片内容
        // readfile($img);
        echo file_get_contents($img);
    }


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图片下载</title>
    <link rel="stylesheet" href="static/css/bootstrap.min.css">
    <link rel="stylesheet" href="static/css/style.css">
</head>
<body>

<?php
    require_once 'header.php';
?>

<div class="container main row">
  <?php
  
    foreach($list as $key=>$val):
  ?>
  <div class="col-sm-6 col-md-4">
    <div class="thumbnail">
      <img src="<?php echo $val;?>" alt="...">
      <div class="caption">
        <p class="text-center">
            <a href="<?php echo $_SERVER['PHP_SELF'].'?img='.$val;?>" class="btn btn-sm btn-success" role="button">下载图片</a> 
            <a target='_blank' href="show.php?img=<?php echo $val;?>" class="btn btn-sm btn-warning" role="button">查看原图</a> 
            <a href="remove.php?img=<?php echo $val;?>" class="btn btn-sm btn-danger" role="button">删除图片</a> 
        </p>
      </div>
    </div>
  </div>
  <?php
    endforeach;
  ?>

</div>

<?php
    require_once 'footer.php';
?>
    
</body>
</html>